//
//  LZSJisuanViewController.m
//  IOSProject
//
//  Created by lzs on 2018/4/20.
//  Copyright © 2018年 com.linzhisheng. All rights reserved.
//

#import "LZSJisuanViewController.h"
#import "LZSJisuanModel.h"
#import "LZSHeaderCell.h"
#import "LZSCaipiaowinningModel.h"
#import "LZSNoDataView.h"

@interface LZSJisuanViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) NSDictionary *dataDic;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) NSMutableArray *realArray;
//
@property (nonatomic, copy) NSString *titleStr;
@property (nonatomic, strong) LZSNoDataView *noview;
@end

@implementation LZSJisuanViewController

-(NSMutableArray *)realArray{
    if (!_realArray) {
        _realArray = [NSMutableArray array];
    }
    return _realArray;
}

-(NSDictionary *)dataDic{
    if (!_dataDic) {
        _dataDic = @{
            @"lottery_id": @"ssq",
            @"lottery_name": @"双色球",
            @"lottery_no": @"18029",
            @"lottery_date": @"2018-03-15",
            @"real_lottery_res": @"01,02,09,14,22,25,05",
            @"lottery_res": @"01,11,02,09,14,22,25|05,03",
            @"in_money": @"28",
            @"buy_red_ball_num": @"7",
            @"buy_blue_ball_num": @"2",
            @"hit_red_ball_num": @"6",
            @"hit_blue_ball_num": @"1",
            @"is_prize": @"1",
            @"prize_msg": @"恭喜您中奖了",
            @"lottery_prize": @[
                              @{
                                  @"prize_name": @"一等奖",
                                  @"prize_require": @"6+1",
                                  @"prize_num": @"1",
                                  @"prize_money": @"8,926,329"
                              },
                              @{
                                  @"prize_name": @"二等奖",
                                  @"prize_require": @"6+0",
                                  @"prize_num": @"1",
                                  @"prize_money":@"288,700"
                              },
                              @{
                                  @"prize_name": @"三等奖",
                                  @"prize_require": @"5+1",
                                  @"prize_num": @"6",
                                  @"prize_money": @"3,000"
                              },
                              @{
                                  @"prize_name": @"四等奖",
                                  @"prize_require": @"4+1,5+0",
                                  @"prize_num": @"6",
                                  @"prize_money": @"200"
                              }
                              ]
        };
    }
    return _dataDic;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUpTopView];
    
//    【
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    //    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = [UIColor lightGrayColor];
    //
    self.tableView.estimatedRowHeight = 80;
    
//    self.tableView.separatorColor = [UIColor colorWithHexString:@"#c8c8c8"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //默认【下拉刷新】
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    //默认【上拉加载】
//    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    //
    self.tableView.tableFooterView = [UIView new];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).mas_offset(0);
    }];
    
    self.tableView.hidden = YES;
    
    LZSNoDataView *nodataView = [[LZSNoDataView alloc] init];
    self.noview = nodataView;
    [self.tableView addSubview:nodataView];
    nodataView.hidden = YES;
    [nodataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView.mas_centerX);
        make.centerY.equalTo(self.tableView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(200, 200));
    }];
    
    //    [self headerSdcycleScrollView];
    
    //
//    [self.tableView.mj_header beginRefreshing];
}

-(void)setUpTopView{
    
    self.topView = [[UIView alloc] initWithFrame:CGRectZero];
    self.topView.backgroundColor = [UIColor whiteColor];
    self.topView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.topView.layer.borderWidth = 0.5;
    [self.view addSubview:self.topView];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(100);
    }];
    
    UILabel *topLabel = [self.view setuplabelwithfont:[UIFont systemFontOfSize:12] withText:@"请输入购买的号码，号码之间用空格隔开，例如：20 03 05 07 22" withColor:[UIColor colorWithHexString:@"0x000000"] wihtAlignemt:NSTextAlignmentLeft];
    topLabel.numberOfLines = 2;
    [self.view addSubview:topLabel];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view).mas_offset(20);
        make.left.equalTo(self.view).mas_offset(20);
        make.right.equalTo(self.view).mas_offset(-20);
    }];
    
    UIView *leftview = [[UIView alloc] init];
    leftview.frame = CGRectMake(0, 0, 10, 10);
    UITextField *topField = [self.view setupTextFieldwithfont:[UIFont systemFontOfSize:15] withPlaceHolder:@"请输入购买的号码" withColor:[UIColor lightGrayColor] wihtAlignemt:NSTextAlignmentLeft withview:leftview];
    self.textField = topField;
    topField.backgroundColor = [UIColor greenColor];
    topField.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:topField];
    [topField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topLabel.mas_bottom).mas_offset(10);
        make.left.equalTo(self.view).mas_offset(20);
        make.right.equalTo(self.view).mas_offset(-100);
        make.height.mas_equalTo(30);
    }];
    
    UIButton *sendBtn = [self.view setupbtnWithFont:[UIFont systemFontOfSize:15] withText:@"查询" withColor:[UIColor lightGrayColor] wihtAlignemt:NSTextAlignmentCenter];
    sendBtn.backgroundColor = [UIColor cyanColor];
    sendBtn.layer.cornerRadius = 15;
    sendBtn.layer.masksToBounds = YES;
    [self.view addSubview:sendBtn];
    [sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(topField.mas_centerY);
        make.left.equalTo(topField.mas_right).mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
    
//    [sendBtn addTarget:self action:@selector(senderBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [sendBtn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
}


-(void)refresh{
    //    [self lotteryBonus];
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

-(void)loadMore{
    //    sleep(2);
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}


-(void)senderBtnClick:(UIButton *)btn{
    
    if (self.textField.text.length == 0) {
        [self.view makeToast:@"购买号码不能为空" duration:0.8 position:CSToastPositionCenter];
        return;
    }
    self.tableView.hidden = NO;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"91b22ffb918dbb3a2b5f98be27009a47" forKey:@"key"];
    [params setValue:self.textField.text forKey:@"lottery_res"];
    [params setValue:self.lottery_id forKey:@"lottery_id"];
    
//    [self postData:@"http://apis.juhe.cn/lottery/bonus" param:params success:^(id responseObject) {
//        LZSJisuanModel *datamodel = [LZSJisuanModel mj_objectWithKeyValues:responseObject[@"result"]];
    LZSJisuanModel *datamodel = [LZSJisuanModel mj_objectWithKeyValues:self.dataDic];
        //是刷新还是加载
//        if (self.page==1) {
            if (datamodel.lottery_prize.count==0) {
                self.noview.hidden = NO;
                self.tableView.hidden = YES;
            }else{
                self.noview.hidden = YES;
                self.tableView.hidden = NO;
            }
            
            [self.realArray removeAllObjects];
            [self.realArray addObjectsFromArray:datamodel.lottery_prize];
//        }else{
//            [self.realArray addObjectsFromArray:datamodel.lotteryResList];
//        }
        
        //是否提示可加载
        if (datamodel.lottery_prize.count<10) {
            // 变为没有更多数据的状态
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            self.tableView.mj_footer.hidden = YES;
        }
//        else{
//            self.page++;
//        }
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
//    } error:^(NSError *error) {
//        [self.view makeToast:error.domain duration:0.8 position:CSToastPositionCenter];
//    }];
    
}

-(void)BtnClick:(UIButton *)btn{
    if (self.textField.text.length == 0) {
        [self.view makeToast:@"购买号码不能为空" duration:0.8 position:CSToastPositionCenter];
        return;
    }
    self.tableView.hidden = NO;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"31771dfbcb6cda3d" forKey:@"appkey"];
    [params setValue:self.textField.text forKey:@"number"];
    [params setValue:self.lottery_id forKey:@"caipiaoid"];
    
    [self postData:@"http://api.jisuapi.com/caipiao/winning" param:params success:^(id responseObject) {
        //        LZSJisuanModel *datamodel = [LZSJisuanModel mj_objectWithKeyValues:responseObject[@"result"]];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"status"] isEqualToString:@"0"]) {
             self.noview.hidden = YES;
            [self.view makeToast:responseObject[@"msg"] duration:0.8 position:CSToastPositionCenter];
            self.titleStr = responseObject[@"msg"];
            [self.realArray removeAllObjects];
            LZSCaipiaowinningModel *datamodel = [LZSCaipiaowinningModel mj_objectWithKeyValues:responseObject[@"result"]];
            
            [self.realArray addObject:datamodel];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
            
        }else{
            self.noview.hidden = NO;
            [self.view makeToast:responseObject[@"msg"] duration:0.8 position:CSToastPositionCenter];
        }
        } error:^(NSError *error) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
             self.noview.hidden = NO;
            [self.view makeToast:error.domain duration:0.8 position:CSToastPositionCenter];
        }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.realArray.count+1;
    return self.realArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.row==0) {
//        LZSHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//        if (!cell) {
//            cell = [[LZSHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        }
//        return cell;
//    }else{
//        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//        if (!cell) {
//            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        }
//        return cell;
//    }
    LZSHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[LZSHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.model = self.realArray[indexPath.row];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    
    UILabel *titleLabel = [self.view setuplabelwithfont:[UIFont systemFontOfSize:15] withText:@"" withColor:[UIColor lightGrayColor] wihtAlignemt:NSTextAlignmentCenter];
    NSString *str = self.titleStr?self.titleStr:@"暂时无法查询";
    titleLabel.text = [NSString stringWithFormat:@"温馨提示:%@",str];
    [headerView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(headerView.mas_centerX);
        make.centerY.equalTo(headerView.mas_centerY);
    }];
    
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
